/*
 * Copyright 2011 yura.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opu.db_vdumper.ui.component.tree;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import org.opu.db_vdumper.resources.ResourceHandle;
import org.opu.db_vdumper.resources.ResourceHandle.IconType;
import org.opu.db_vdumper.ui.component.area.model.CanvesModel;
import org.opu.db_vdumper.ui.component.tabel.models.Model;
import org.opu.db_vdumper.ui.component.tabel.models.SelectedComponentImpl;

/**
 *
 * @author yura
 */
public class TreeComponent extends SelectedComponentImpl<Model> {
	public static final int BUTTON_SIZE = 20;

    private Model viewModel;
    private CanvesModel canvesModel;
    private int step;
    private List<JComponent> childrens;
    private JCheckBox checkBox;
    private Component space;
    private Button button;
    private Component buttonSpace;
    private boolean hidedChildren = false;

    public TreeComponent(CanvesModel canvesModel, Model tableModel, int step) {
        this.step = step;
        this.childrens = new ArrayList<JComponent>();
        this.checkBox = new JCheckBox(tableModel != null ? "" + tableModel : null);
        this.button = new Button();
        this.space = Box.createHorizontalStrut(step);
        this.buttonSpace = Box.createHorizontalStrut(BUTTON_SIZE);
        this.canvesModel = canvesModel;

        setLayout(new BorderLayout());
        setDoubleBuffered(true);
        setBackground(Color.WHITE);

        setViewModel(tableModel);

        this.checkBox.setBackground(Color.WHITE);
        this.checkBox.addActionListener(new ActionListenerImpl());
    }

    public int getStep() {
        return step;
    }

    public void setStep(int step) {
        this.step = step;
    }

    public CanvesModel getCanvesModel() {
        return canvesModel;
    }

    public void setCanvesModel(CanvesModel canvesModel) {
        this.canvesModel = canvesModel;
    }

    public void setViewModel(Model tableModel) {
        this.viewModel = tableModel;

        if (this.viewModel != null) {
            tableModel.addViews(this);
        }

        reset();
    }

    @Override
    public Model getViewModel() {
        return viewModel;
    }

    public List<JComponent> getChildrens() {
        return childrens;
    }

    public void setChildrens(List<JComponent> childrens) {
        this.childrens = childrens;
    }

    public boolean addChildren(JComponent e) {
    	boolean b = childrens.add(e);
    	childrenModed();
    	return b;
    }
    
    public boolean remove(Object o) {
    	boolean b =  childrens.remove(o);
    	childrenModed();
    	return b;
	}

	@Override
    public void reset() {
        checkBox.setSelected(viewModel.isSelected());
        checkBox.setText(viewModel != null ? "" + viewModel : null);

        this.space = Box.createHorizontalStrut(step);
        
        if (childrens.isEmpty()){
        	button.setVisible(false);
        }
        
        initComponent();
        revalidate();
        repaint();
    }

    @Override
    public void setVisible(boolean aFlag) {
        super.setVisible(aFlag);
        setHidedChildren(hidedChildren);
    }

    public boolean isHidedChildren() {
        return hidedChildren;
    }

    public void setHidedChildren(boolean hided) {
        this.hidedChildren = hided;

        for (JComponent ch : childrens) {
            ch.setVisible(hided);
        }

        button.setIcon(hided? button.min: button.pls);
    }
    
    protected void childrenModed(){
    	button.setVisible(!childrens.isEmpty());
    	buttonSpace.setVisible(childrens.isEmpty());
    }

    private void initComponent() {
        removeAll();

        Box box = Box.createHorizontalBox();
        box.add(space);
        box.add(button);
        box.add(buttonSpace);
        box.add(Box.createHorizontalStrut(2));
        box.add(checkBox);
        box.setBackground(Color.WHITE);

        add(box, BorderLayout.CENTER);
    }

    private class ActionListenerImpl implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (canvesModel != null && viewModel != canvesModel) {
                canvesModel.setSelected(viewModel, checkBox.isSelected());
            }
        }
    }

    private class Button extends JButton {

        private static final long serialVersionUID = 5658342588823554908L;
		
        Icon min = ResourceHandle.getIcon("-.png", IconType.small);
        Icon pls = ResourceHandle.getIcon("+.png", IconType.small);

        public Button() {
            this.setHideActionText(true);
            this.setBorderPainted(false);
            this.setContentAreaFilled(false);
            this.setCursor(new Cursor(Cursor.HAND_CURSOR));
            this.setFocusPainted(false);
            
            this.setMaximumSize(new Dimension(BUTTON_SIZE, BUTTON_SIZE));
            this.setIcon(min);

            this.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    setHidedChildren(!isHidedChildren());
                }
            });
        }
    }
}
